package cn.cmcc.online.smsapi.ui.util;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;

import org.json.JSONArray;
import org.json.JSONObject;

import cn.cmcc.online.smsapi.ui.manager.IDataCallback;
import cn.cmcc.online.smsapi.ui.manager.IDataExchange;
import cn.cmcc.online.smsapi.ui.model.ActionData;
import cn.cmcc.online.smsapi.ui.model.FlightData;
import cn.cmcc.online.smsapi.ui.model.TrainData;
import cn.cmcc.online.smsapi.ui.web.IActivityForWebView;
import cn.cmcc.online.smsapi.ui.web.IActivityParamForJS;

public class DataExchangeProxy implements IDataExchange {
    private IDataExchange mDataExchange = null;

    @Override
    public void processAction(Context activityContext, ActionData action) {
        if (mDataExchange != null) {
            mDataExchange.processAction(activityContext, action);
        }
    }

    @Override
    public void setTrainInfoListener(IDataCallback<JSONObject> callback) {
        if (mDataExchange != null) {
            mDataExchange.setTrainInfoListener(callback);
        }
    }

    @Override
    public void getTrainInfo(TrainData data) {
        if (mDataExchange != null) {
            mDataExchange.getTrainInfo(data);
        }
    }

    @Override
    public void getFlightInfo(FlightData data, IDataCallback<FlightData> callback) {
        if (mDataExchange != null) {
            mDataExchange.getFlightInfo(data, callback);
        }
    }

    @Override
    public void getMetaInfo(JSONObject data, IDataCallback<JSONObject> callback) {
        if (mDataExchange != null) {
            mDataExchange.getMetaInfo(data, callback);
        }
    }

    @Override
    public void updateCardInfo(JSONObject data) {
        if (mDataExchange != null) {
            mDataExchange.updateCardInfo(data);
        }
    }

    @Override
    public BitmapDrawable getBitmap(String path) {
        BitmapDrawable drawable = null;
        if (mDataExchange != null) {
            drawable = mDataExchange.getBitmap(path);
        }

        return drawable;
    }

    @Override
    public IActivityParamForJS getJSManager(IActivityForWebView activityForWebView) {
        if (mDataExchange != null) {
            return mDataExchange.getJSManager(activityForWebView);
        }
        return null;
    }

    @Override
    public boolean geOnOffByType(int type) {
        if (mDataExchange != null) {
            return mDataExchange.geOnOffByType(type);
        }
        return true;
    }

    @Override
    public JSONArray getConfigByType(int type, String uiversion, Integer suportMenuId) {
        if (mDataExchange != null) {
            return mDataExchange.getConfigByType(type, uiversion, suportMenuId);
        }
        return null;
    }

    @Override
    public int checkNetWork(Context context, int type) {
        if (mDataExchange != null) {
            return mDataExchange.checkNetWork(context, type);
        }
        return -1;
    }

    @Override
    public void openMap(Context context, String name, String address, Double longitude, Double latitude) {
        if (mDataExchange != null) {
            mDataExchange.openMap(context, name, address, longitude, latitude);
        }
    }

    @Override
    public void getLocation(Context context, Handler handler) {
        if (mDataExchange != null) {
            mDataExchange.getLocation(context, handler);
        }
    }

    private DataExchangeProxy(IDataExchange dataExchange) {
        mDataExchange = dataExchange;
    }

    private static DataExchangeProxy sDataExchangeProxy = null;

    public static DataExchangeProxy getInstance() {
        return sDataExchangeProxy;
    }

    public static DataExchangeProxy getInstance(IDataExchange dataExchange) {
        synchronized (DataExchangeProxy.class) {
            if (sDataExchangeProxy == null) {
                sDataExchangeProxy = new DataExchangeProxy(dataExchange);
            }
        }

        return sDataExchangeProxy;
    }
}